<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>商城名称</p>
          <el-input v-model="Api_GET_ClubTAManagementQO.StoreNameFilter" placeholder="商城名称" size="mini" clearable @change="gettablelist()" />
        </el-col>
        <el-col :span="4">
          <p>机构名称</p>
          <el-input v-model="Api_GET_ClubTAManagementQO.Filter" placeholder="机构名称" size="mini" clearable @change="gettablelist()" />
        </el-col>
        <el-col :span="4">
          <p>地区</p>
          <el-select v-model="provinceCode" placeholder="请选择省份" :disabled="selected[0]" size="mini" filterable @change="changeProvince">
            <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="cityCode" placeholder="请选择城市" :disabled="selected[1]" class="search_btn" size="mini" filterable @change="changeCity">
            <el-option v-for="item in cityList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="countryCode" placeholder="请选择区域" :disabled="selected[2]" class="search_btn" size="mini" filterable @change="changeCounty">
            <el-option v-for="item in countryList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" style="margin-top:25px" size="mini" @click="gettablelist()">筛选</el-button>
          <el-button type="warning" size="mini" @click="reset()">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" size="mini" :data="tableData" class="gl_table_noBorder gl_mt_10 gl_mb_15" style="width: 100%">
      <el-table-column label="序号" type="index" align="center" width="55" />
      <el-table-column label="商城名称" prop="storeName" align="center">
        <template slot-scope="{row}">
          <span>{{ row.storeName }}</span> <i v-if="row.isDisabled" type="text" class="el-icon-remove" />
        </template>
      </el-table-column>
      <el-table-column label="上架商品" prop="productCount" align="center" />
      <el-table-column label="销售额(元)" align="center">
        <template slot-scope="{row}">
          {{ (row.totalAmount / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="机构名称" prop="unitName" align="center" />
      <el-table-column label="机构地址" min-width="160">
        <template slot-scope="{ row }">
          {{ addressDataForCheck(row) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" :width="130">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="lookgoods(row)">查看</el-button>
          <span v-if="user==='WXCenter'" style="margin-left:10px">
            <el-button v-if="row.isDisabled" size="mini" type="text" @click="enable(row)">解禁</el-button>
            <el-button v-else size="mini" type="text" @click="ban(row)">
              <span class="gl_color_error">
                禁用
              </span>
            </el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_pt_15" style="text-align: right;">
      <el-pagination
        :current-page="Api_GET_ClubTAManagementQO.Page"
        :page-size="Api_GET_ClubTAManagementQO.MaxResultCount"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>
    <ban ref="ban" />
  </div>
</template>

<script>
import ban from '../../ban/index';
import { mapState, mapActions } from 'vuex';
import { api_PUT_ClubTAManagementDisableStore } from '@/extend/api/hallswaggerApi/app.js';
import { DisableStoreInput } from '@/lib/form/hall/swaggerForm';
import { api_GET_ClubTAManagement } from '@/extend/api/hallswaggerApi/app.js';
import { Api_GET_ClubTAManagementQO } from '@/lib/form/hall/swaggerForm';
import {
  api_GET_Provinces,
  api_GET_CitiesProvinceCode,
  api_GET_CountiesCityCode
} from '@/extend/api/gameswaggerApi/district.js';
export default {
  components: {
    ban
  },
  data() {
    return {
      infoCity: {},
      selected: [false, false, false],
      DisableStoreInput: new DisableStoreInput(),
      Api_GET_ClubTAManagementQO: new Api_GET_ClubTAManagementQO(),
      totalPage: 0,
      sex: '',
      loading: false,
      tableData: [],
      provinceCode: null, // 地区c
      cityCode: null,
      countryCode: null,
      provinceList: [],
      cityList: [],
      countryList: []
    };
  },
  computed: {
    ...mapState({
      info: state => state.user.info,
      dict: state => state.dict.dict,
      cache: state => state.dict.cache,
      user: state => state.user.info.currentOrganizationUnit.typeCode
    })
  },
  created() {
    this.infoCity = JSON.parse(this.$route.query.infoCity);
    this.init();
    this.apiGetProvinces();
    this.gettablelist();
    this.getDictByType(['district']);
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    init() {
      if (this.infoCity) {
        this.selected[0] = Boolean(this.infoCity.provinceCode);
        this.selected[1] = Boolean(this.infoCity.cityCode);
        this.selected[2] = Boolean(this.infoCity.countyCode);
      }
      this.provinceCode = this.infoCity.provinceCode;
      this.cityCode = this.infoCity.cityCode;
      this.countryCode = this.infoCity.countyCode;
      this.provinceCode ? this.apiGetCitiesByProvince() : '';
      this.cityCode ? this.apiGetCountiesByCity() : '';
    },
    // 获取 Provinces 字典
    apiGetProvinces () {
      api_GET_Provinces()
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.provinceList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    // 获取 Cities 字典
    apiGetCitiesByProvince () {
      api_GET_CitiesProvinceCode(this.provinceCode)
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.cityList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    // 获取 Counties 字典
    apiGetCountiesByCity () {
      api_GET_CountiesCityCode(this.cityCode)
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.countryList = res;
          }, error => {
            this.tableLoading = false;
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    gettablelist(page) {
      this.loading = true;
      this.Api_GET_ClubTAManagementQO.IsDisabled = '';
      this.Api_GET_ClubTAManagementQO.ProvinceCode = this.provinceCode;
      this.Api_GET_ClubTAManagementQO.CityCode = this.cityCode;
      this.Api_GET_ClubTAManagementQO.CountyCode = this.countryCode;
      this.Api_GET_ClubTAManagementQO.Sorting = 'creationTime DESC';
      this.Api_GET_ClubTAManagementQO.Page = page || 1;
      this.Api_GET_ClubTAManagementQO.SkipCount = (this.Api_GET_ClubTAManagementQO.Page - 1) * this.Api_GET_ClubTAManagementQO.MaxResultCount;
      api_GET_ClubTAManagement(this.Api_GET_ClubTAManagementQO).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.totalPage = res.totalCount;
      }
      , error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    changeProvince(province) {
      this.cityCode = '';
      this.countryCode = '';
      this.cityList = [];
      this.countryList = [];
      if (province !== null) {
        this.cityCode = null;
        this.apiGetCitiesByProvince();
      }
      this.gettablelist();
    },
    changeCity(city) {
      this.countryCode = '';
      this.countryList = [];
      if (city !== null) {
        this.countryCode = null;
        this.apiGetCountiesByCity();
      }
      this.gettablelist();
    },
    changeCounty() {
      this.gettablelist();
    },
    // 重置
    reset() {
      this.init();
      this.Api_GET_ClubTAManagementQO = new Api_GET_ClubTAManagementQO();
      // this.provinceCode = null;
      // this.cityCode = null;
      // this.countryCode = null;
      // this.cityList = [];
      // this.countryList = [];
      // this.GenderCode = null;
      this.gettablelist();
    },
    // 查看商品
    lookgoods(row) {
      this.$router.push({ name: 'Lookgoods', query: { storeid: row.organizationUnitId, organizationUnitId: this.$route.query.organizationUnitId }});
    },
    // 分页按钮
    sizeChange(MaxResultCount) {
      this.Api_GET_ClubTAManagementQO.MaxResultCount = MaxResultCount;
      this.gettablelist();
    },
    pageIndexChange(currentPage) {
      this.gettablelist(currentPage);
    },
    // 启用
    enable(row) {
      this.loading = true;
      this.DisableStoreInput.isDisabled = false;
      this.DisableStoreInput.storeId = row.storeId;
      api_PUT_ClubTAManagementDisableStore(this.DisableStoreInput).then(res => {
        this.loading = false;
        this.$message({
          type: 'success',
          message: '解禁成功'
        });
        this.gettablelist();
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 机构地址
    addressDataForCheck (memberInfo) {
      const _provinceCode = memberInfo.provinceCode;
      const _cityCode = memberInfo.cityCode;
      const _countyCode = memberInfo.countyCode;
      let _provinceData;
      let _cityData;
      let _countyData;
      if (_provinceCode && this.cache.district) {
        _provinceData = this.cache.district.find(item => item.code === _provinceCode);
      }
      if (_cityCode && _provinceData) {
        _cityData = _provinceData.children.find(item => item.code === _cityCode);
      }
      if (_countyCode && _cityData) {
        _countyData = _cityData.children.find(item => item.code === _countyCode);
      }
      return `${_provinceData ? _provinceData.name : ''} ${_cityData ? _cityData.name : ''} ${_countyData ? _countyData.name : ''}`;
    },
    // 禁用功能
    ban(row) {
      console.log(row);
      this.$refs.ban.dialogVisible = true;
      this.$refs.ban.DisableStoreInput.storeId = row.storeId;
      this.$refs.ban.DisableStoreInput.isDisabled = true;
    }
  }

};
</script>
<style scoped>

</style>;
